package xyz.ssf.cloud.common.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.List;

/**
 * 密码工具类
 * 
 * @author ssf
 */
@Component
public class PasswordUtil {
    
    private static BCryptPasswordEncoder passwordEncoder;
    
    @Autowired
    public void setPasswordEncoder(BCryptPasswordEncoder passwordEncoder) {
        PasswordUtil.passwordEncoder = passwordEncoder;
    }
    
    /**
     * 加密
     * 
     * @param password 明文密码
     * @return 加密后的密码
     */
    public static String encode(String password) {
        return passwordEncoder.encode(password);
    }
    
    /**
     * 验证密码
     * 
     * @param rawPassword 明文密码
     * @param encodedPassword 加密后的密码
     * @return 是否匹配
     */
    public static boolean matches(String rawPassword, String encodedPassword) {
        return passwordEncoder.matches(rawPassword, encodedPassword);
    }
    
    /**
     * 获取用户ID
     */
    public static Long getUserId() {
        try {
            // 从Spring Security上下文中获取当前登录用户信息
            org.springframework.security.core.Authentication authentication = 
                org.springframework.security.core.context.SecurityContextHolder.getContext().getAuthentication();
            if (authentication != null && authentication.isAuthenticated() && 
                !"anonymousUser".equals(authentication.getPrincipal())) {
                // 根据实际用户对象的结构获取ID
                Object principal = authentication.getPrincipal();
                if (principal instanceof org.springframework.security.core.userdetails.UserDetails) {
                    // 假设UserDetails实现类中有getUserId方法，或通过getName获取
                    // 这里需要根据实际的UserDetails实现类做调整
                    return Long.parseLong(((org.springframework.security.core.userdetails.UserDetails) principal).getUsername());
                }
            }
            // 如果无法获取，返回默认值
            return 1L;
        } catch (Exception e) {
            throw new RuntimeException("获取用户ID异常", e);
        }
    }
    
    /**
     * 获取用户名
     */
    public static String getUsername() {
        try {
            // 从Spring Security上下文中获取当前登录用户信息
            org.springframework.security.core.Authentication authentication = 
                org.springframework.security.core.context.SecurityContextHolder.getContext().getAuthentication();
            if (authentication != null && authentication.isAuthenticated() && 
                !"anonymousUser".equals(authentication.getPrincipal())) {
                // 获取用户名
                return authentication.getName();
            }
            // 如果无法获取，返回默认值
            return "admin";
        } catch (Exception e) {
            throw new RuntimeException("获取用户名异常", e);
        }
    }
    
    /**
     * 判断字符串是否为非空
     *
     * @param str 字符串
     * @return 结果
     */
    public static boolean isNotEmpty(String str) {
        return str != null && !str.isEmpty();
    }
    
    /**
     * 判断集合是否为非空
     *
     * @param collection 集合
     * @return 结果
     */
    public static boolean isNotEmpty(Collection<?> collection) {
        return collection != null && !collection.isEmpty();
    }
    
    /**
     * 生成加密后的密码
     * 
     * @param args 参数
     */
    public static void main(String[] args) {
        System.out.println(encode("admin123"));
    }
}